<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="com.google.appengine.codelab.*" %>

<%@page import="java.util.*" %>
<%@page import="java.math.BigDecimal" %>
<%@page import="com.google.appengine.api.datastore.Entity" %>




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<jsp:include page="/WEB-INF/jsp/head.jsp"/>

  <script>

$(function() {

$( "#dataInicial" ).datepicker();

});



$(function() {

$( "#dataFinal" ).datepicker();

});



</script>
</head>

<body>



<jsp:include page="/WEB-INF/jsp/cabecalhoScaf.jsp"/>
<script >$("#divTitle").html("User Story 10 e 11");</script>



	<form method="POST" action="us10-11.jsp">	
		Selecione o tipo de relatório: 
		<select name="TipoRel" id="TipoRel">
			<option>-------</option>
			<option value="Movel/Desktop">Móvel/Desktop</option>
			<option value="SO">Sistemas Operacionais</option>
			<option value="Navegador">Navegadores</option>
		</select>
		<jsp:include page="/WEB-INF/jsp/common/RangeDatas.jsp" />	
		<input type="submit">
	</form>
	
	
<%
//UserStory10Servlet.insertEntities(); //Carrega a massa de dados / comentado


String DataIni = request.getParameter("dataInicial");
String DataFin = request.getParameter("dataFinal");

String dataScafInia = null;
String dataScafFina = null;
int i=0;
String dataScafIni = (String)session.getAttribute("DataIni");
String dataScafFin = (String)session.getAttribute("DataFin");




String TipoRel = request.getParameter("TipoRel"); //Recebe a seleção do usuário
//out.println(TipoRel);

if(DataIni !=null && !DataIni.equals("") && DataFin !=null && !DataFin.equals("")){
	dataScafInia = Common.StrDateToDataScaf(DataIni);
	dataScafFina = Common.StrDateToDataScaf(DataFin);
	session.setAttribute("DataIni", dataScafInia);
	session.setAttribute("DataFin", dataScafFina);
	
	dataScafIni = (String)session.getAttribute("DataIni");
	dataScafFin = (String)session.getAttribute("DataFin");	
	
}

	if(TipoRel !=null && !TipoRel.equals("") && dataScafIni !=null && !dataScafIni.equals("") && dataScafFin !=null && !dataScafFin.equals("")){
	
		
	
/////////////////////////////////RELATORIO POR SO////////////////////////////////			
		if(TipoRel.equals("SO")){
					long total = UserStory10Servlet.totalFraudesByInternetBanking(dataScafIni,dataScafFin);
					%>
					<table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
		           
		              <tr>
		                <th colspan="5"><div align="center">Fraudes por Sistema Operacional</div></th>
		              </tr>
		              <tr>
		                <th scope="col">Sistema Operacional</th>
		                <th scope="col">Versão</th>
		                <th scope="col">Móvel</th>
		                <th scope="col">Quantidade</th>
		                <th scope="col">%</th>
		              </tr>
		            <% 
					
					for (Entity sis : new SistemaOperacional().getAll().asIterable()) {
						String sis_nome = sis.getProperty("sis_nome").toString();
						String sis_versao = sis.getProperty("sis_versao").toString();
						String sis_movel = sis.getProperty("sis_movel").toString().equals("1") ? "Sim" : "Não";
						long count = UserStory10Servlet.countFraudesBySistemaOperacional(sis_nome, sis_versao, "", dataScafIni,dataScafFin);
						String Porcent =  String.format("%.2f%%", new Float((count * 100) / (double) total));
						if(count>0){ //se tiver registro de fraude, insere link para o DrillDown
						out.println("<tr> <td> <a href='us10-11.jsp?TipoRel=DD-NAV-SO&SO=" + sis_nome + "&SOVer=" + sis_versao + "&Movel=" + sis_movel + "'>"
									+ sis_nome
									+ "</a></td><td>"
									+ sis_versao
									+ "</td><td>"
									+ sis_movel
									+ "</td><td>"
									+ count
									+ "</td><td>"
									+ Porcent
								);} else { //Senão, exibe sem o link
									out.println("<tr> <td>"
											+ sis_nome
											+ "</td><td>"
											+ sis_versao
											+ "</td><td>"
											+ sis_movel
											+ "</td><td>"
											+ count
											+ "</td><td>"
											+ Porcent
										);
											}
									
									
								
						
						
																						}//for (Entity sis : new SistemaOperacional().getAll().asIterable()) 
		            	out.println("</table>");
		            
										} //if(TipoRel.equals("SO")){

										else
/////////////////////////////////RELATORIO POR NAVEGADOR////////////////////////////////												
											if(TipoRel.equals("Navegador")){
											
												long total = UserStory11Servlet.countFraudesByNavegador("", "");
										          %> <table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
										            <thead>
										              <tr>
										                <th colspan="4"><div align="center">Fraudes por Navegador</div></th>
										              </tr>
										              <tr>
										                <th scope="col">Navegador</th>
										                <th scope="col">Versão</th>
										                <th scope="col">Quantidade</th>
										                <th scope="col">%</th>
										              </tr>
										            </thead><%
												for (Entity sis : new Navegador().getAll().asIterable()) {
													String nav_nome = sis.getProperty("nav_nome").toString();
													String nav_versao = sis.getProperty("nav_versao").toString();
													long count = UserStory11Servlet.countFraudesByNavegador(nav_nome, nav_versao);
													String Porcent =  String.format("%.2f%%", new Float((count * 100) / (double) total));
													
													out.println("<tr> <td>"
															+ nav_nome
															+ "</td><td>"
															+ nav_versao
															+ "</td><td>"
															+ count
															+ "</td><td>"
															+ Porcent
														);
													
													
																											}//for (Entity sis : new Navegador().getAll().asIterable()) 
										            out.println("</table>");
												
											} //if(TipoRel.equals("Navegador")){
												
											else
/////////////////////////////////RELATORIO POR MOVEL X DESKTOP////////////////////////////////													
												if(TipoRel.equals("Movel/Desktop")){
													long total = UserStory10Servlet.totalFraudesByInternetBanking(dataScafIni,dataScafFin);
													long count = UserStory10Servlet.countFraudesBySistemaOperacional("", "", "0", dataScafIni, dataScafFin);
													%>
													          <table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
        														    <thead>
          															    <tr>
               															 <th colspan="3"><div align="center">Fraudes por Tipo (Desktop / Móvel) de Sistema Operacional</div></th>
           																   </tr>
            																	  <tr>
            																	    <th scope="col">Desktop / Móvel</th>
             																	    <th scope="col">Total</th>
                																	<th scope="col">%</th>
                																		</tr>
               																			 </thead>
                																			<tr><td><a href=us10-11.jsp?TipoRel=SODesktop> Desktop </a> </td><td> <%=count%> </td> <td> <%=String.format("%.2f%%", new Float((count * 100) / (double) total))  %> </td></tr>
               																					 <tr><td> <a href=us10-11.jsp?TipoRel=SOMovel> Móvel</a> </td><td> <%=(total - count) %> </td><td><%=String.format("%.2f%%",new Float(((total - count) * 100) / (double) total)) %></td></tr>
																					<% } //if(TipoRel.equals("Movel/Desktop")){
											 else
/////////////////////////////////RELATORIO POR SO MOVEL////////////////////////////////													 
												 if(TipoRel.equals("SOMovel")){
													 
														long total = UserStory10Servlet.countFraudesBySistemaOperacional("", "", "1", dataScafIni, dataScafFin);
														%>
														<table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
											           
											              <tr>
											                <th colspan="5"><div align="center">Fraudes por Sistema Operacional</div></th>
											              </tr>
											              <tr>
											                <th scope="col">Sistema Operacional</th>
											                <th scope="col">Versão</th>
											                <th scope="col">Móvel</th>
											                <th scope="col">Quantidade</th>
											                <th scope="col">%</th>
											              </tr>
											            <% 
														
														for (Entity sis : new SistemaOperacional().getAll().asIterable()) {
															String sis_nome = sis.getProperty("sis_nome").toString();
															String sis_versao = sis.getProperty("sis_versao").toString();
															String sis_movel = sis.getProperty("sis_movel").toString().equals("1") ? "Sim" : "Não";
															long count = UserStory10Servlet.countFraudesBySistemaOperacional(sis_nome, sis_versao, "", dataScafIni, dataScafFin);
															String Porcent =  String.format("%.2f%%", new Float((count * 100) / (double) total));
															if(count>0 && sis_movel.equals("Sim")){
															out.println("<tr> <td> <a href='us10-11.jsp?TipoRel=DD-NAV-SO&SO=" + sis_nome + "&SOVer=" + sis_versao + "&Movel=" + sis_movel + "'>"
																		+ sis_nome
																		+ "</a></td><td>"
																		+ sis_versao
																		+ "</td><td>"
																		+ sis_movel
																		+ "</td><td>"
																		+ count
																		+ "</td><td>"
																		+ Porcent
																	);} 
																		
																		
																	
															
															
																															}//for (Entity sis : new SistemaOperacional().getAll().asIterable()) 
											            	out.println("</table>");	 
													 
												 }// if(TipoRel.equals("SOMovel")){
													 
												 else
/////////////////////////////////RELATORIO POR SO Desktop////////////////////////////////													 
													 if(TipoRel.equals("SODesktop")){
											
													 
														 
															long total = UserStory10Servlet.countFraudesBySistemaOperacional("", "", "0", dataScafIni,dataScafFin);
															%>
															<table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
												           
												              <tr>
												                <th colspan="5"><div align="center">Fraudes por Sistema Operacional</div></th>
												              </tr>
												              <tr>
												                <th scope="col">Sistema Operacional</th>
												                <th scope="col">Versão</th>
												                <th scope="col">Móvel</th>
												                <th scope="col">Quantidade</th>
												                <th scope="col">%</th>
												              </tr>
												            <% 
															
															for (Entity sis : new SistemaOperacional().getAll().asIterable()) {
																String sis_nome = sis.getProperty("sis_nome").toString();
																String sis_versao = sis.getProperty("sis_versao").toString();
																String sis_movel = sis.getProperty("sis_movel").toString().equals("1") ? "Sim" : "Não";
																long count = UserStory10Servlet.countFraudesBySistemaOperacional(sis_nome, sis_versao, "", dataScafIni,dataScafFin);
																String Porcent =  String.format("%.2f%%", new Float((count * 100) / (double) total));
																if(count>0 && sis_movel.equals("Não")){
																out.println("<tr> <td> <a href='us10-11.jsp?TipoRel=DD-NAV-SO&SO=" + sis_nome + "&SOVer=" + sis_versao + "&Movel=" + sis_movel + "'>"
																			+ sis_nome
																			+ "</a></td><td>"
																			+ sis_versao
																			+ "</td><td>"
																			+ sis_movel
																			+ "</td><td>"
																			+ count
																			+ "</td><td>"
																			+ Porcent
																		);} 
																			
																			
																		
																
																
																																}//for (Entity sis : new SistemaOperacional().getAll().asIterable()) 
												            	out.println("</table>");	 
														 
													 }// if(TipoRel.equals("SODesktop")){
													
													 else
/////////////////////////////////RELATORIO POR DrillDown Navegadores por SO////////////////////////////////															 
														 if(TipoRel.equals("DD-NAV-SO")){
															 String SO = request.getParameter("SO");
														 		String SOVer = request.getParameter("SOVer");
														 		String Movel = request.getParameter("Movel");
														 		String MovelDisp = Movel; 
														 		
																								%>
																		          <table width="100%" cellspacing="0" cellpadding="2" border="1" style="border-collapse:collapse;">
														          <thead>
														              <tr>
														                <th colspan="7"><div align="center">Navegadores em Fraudes pelo Sistema Operacional <%=SO%> Versão <%=SOVer%></div></th>
														              </tr>
														              <tr>
														                <th scope="col">Sistema Operacional</th>
														                <th scope="col">Versão</th>
														                <th scope="col">Móvel</th>
														                <th scope="col">Navegador</th>
														                <th scope="col">Versão</th>
														                <th scope="col">Quantidade</th>
														                <th scope="col">%</th>
														              </tr>
														              </thead>
																            <% 
																			
																 
																    		if(Movel.equals("Não"))
																    			Movel = "0";
																    		else
																    			if(Movel.equals("Sim"))
																    				Movel = "1";
																    		
																        	long total = UserStory10Servlet.countFraudesBySistemaOperacional(SO, SOVer, Movel, dataScafIni, dataScafFin);
																    		
																    		
																    		for (Entity sis : new Navegador().getAll().asIterable()) {
																    			String Navegador = sis.getProperty("nav_nome").toString();
																    			String NavegadorVer = sis.getProperty("nav_versao").toString();
																    		
																    		//String Navegador = "Internet Explorer";
																    		//String NavegadorVer = "9.0";
																    		long count = UserStory10Servlet.countFraudesBySistemaOperacionalAndNavegador(SO, SOVer, Movel,
																    				Navegador, NavegadorVer, dataScafIni,dataScafFin);
																    		String Porcent =  String.format("%.2f%%", new Float((count * 100) / (double) total));
																            
																    		if(count > 0 ){
																    		out.println("<tr> <td>"
																					+ SO
																					+ "</td><td>"
																					+ SOVer
																					+ "</td><td>"
																					+ MovelDisp
																					+ "</td><td>"
																					+ Navegador
																					+ "</td><td>"
																					+ NavegadorVer
																					+ "</td><td>"
																					+ count
																					+ "</td><td>"
																					+ Porcent
																				);	}		
																    																} //for (Entity sis : new Navegador().getAll().asIterable()) {		

															 
														 }// if(TipoRel.equals("SODesktop")){
														 
													 
												 
											
											
											
												} //if(TipoRel !=null && !TipoRel.equals("")){
%>



</body>